<?php 
class SurveySection
{
	public $Id;
	public $SurveyId;
	public $Type;
	public $Order;
	public $Active;
	public $Title;
	public $Guide;
	public $Questions;
	public $ActiveQuestions;
	public $SubLoaded;
	public $DataChanged;
	
	public function SurveySection() 
	{ 
		self::setSubLoaded(false);
		self::setDataChanged(false);
	}
	
	public function getId() { return $this->Id; }
	public function getSurveyId() { return $this->SurveyId; }
	public function getType() { return $this->Type; }
	public function getOrder() { return $this->Order; }
	public function getActive() { return $this->Active; }
	public function getTitle() { return $this->Title; }
	public function getGuide() { return $this->Guide; }
	public function getQuestions() { return $this->Questions; }
	public function getActiveQuestions() { return $this->ActiveQuestions; }
	public function getSubLoaded() { return $this->SubLoaded; }
	public function getDataChanged() { return $this->DataChanged; }
	
	public function setId($val) { $this->Id=$val; }
	public function setSurveyId($val) { $this->SurveyId=$val; }
	public function setType($val) { $this->Type=$val; }
	public function setOrder($val) { $this->Order=$val; }
	public function setActive($val) { $this->Active=$val; }
	public function setTitle($val) { $this->Title=$val; }
	public function setGuide($val) { $this->Guide=$val; }
	public function setQuestions($val) { $this->Questions=$val; }
	public function setActiveQuestions($val) { $this->ActiveQuestions=$val; }
	public function setSubLoaded($val) { $this->SubLoaded=$val; }
	public function setDataChanged($val) { $this->DataChanged=$val; }
	
	public function loadQuestions()
	{
		if(($this->Id > 0) && ($this->SubLoaded == false))
		{
			unset($this->Questions);
			unset($this->ActiveQuestions);
			
			$sql  = 'SELECT `id`, `survey_id`, `section_id`, `question_type`, `question`, `min_val`, `max_val`, `question_order`, `active` ';
			$sql .= 'FROM '.DB_PERFIX.'survey_question WHERE section_id='.$this->Id.' ORDER BY `section_id`, `question_order`, `id` ASC;';
			
			$result = mysql_query($sql);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{
					$this->Questions[$row['id']] = new SurveyQuestion();
					$this->Questions[$row['id']]->setId($row['id']);
					$this->Questions[$row['id']]->setSurveyId($this->Id);
					$this->Questions[$row['id']]->setSectionId($row['section_id']);
					$this->Questions[$row['id']]->setType($row['question_type']);
					$this->Questions[$row['id']]->setOrder($row['question_order']);
					$this->Questions[$row['id']]->setActive($row['active']);
					$this->Questions[$row['id']]->setQuestion($row['question']);
					$this->Questions[$row['id']]->setMinValue($row['min_val']);
					$this->Questions[$row['id']]->setMaxValue($row['max_val']);
					if($row['active'] > 0)
					{ $this->ActiveQuestions++; }
				}
			}
			$this->setSubLoaded(true);
		}
	}
	
	public function update()
	{
		if(($this->Id > 0) && ($this->DataChanged == true))
		{
			$sql  = 'UPDATE '.DB_PERFIX.'survey_sections SET ';
			$sql .= '`title`=\''.mysql_real_escape_string($this->Title).'\', ';
			if(isset($this->Guide))
			{ $sql .= '`guide`=\''.$this->Guide.'\', '; }
			$sql .= '`section_type`='.$this->Type.' ';
			$sql .= 'WHERE `id`='.$this->Id.';';
			$result = mysql_query($sql);
		}
	}
}
?>